from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from netbook.auth.auth_forms import LoginForm
from netbook.utils import *

def login(request):
    if request.method == 'POST':
	form = LoginForm(request.POST)
	if form.is_valid():
	    cd = form.cleaned_data
	    try:
		session = api.login(mail=cd['mail'],passwd=cd['passwd'])
	    except SOAPpy.faultType as ft:
		form = LoginForm()
	    else:
		request.session["uuid"] = session
		request.session['mail']= cd['mail']
		return HttpResponseRedirect('/note/my/')
    else:
	form = LoginForm()
    return render_to_response('login_form.html',{'form':form})

def logout(request):
    try:
	session = api.logout(session=request.session.get('uuid',''))
    except SOAPpy.faultType as ft:
	pass
    else:
	del request.session["uuid"];
	del request.session['mail'];
    return HttpResponseRedirect('/index/')
